package com.test.mooan.labama.utils_boom.helper_boom;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.test.mooan.labama.activity_fragment_boom.base_boom.BaseApplication_boom;
import com.test.mooan.labama.constants_boom.Constant_boom;
import com.test.mooan.labama.utils_boom.Stringutil_boom;
import com.test.mooan.labama.utils_boom.http_boom.HttpManager_boom;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;


/**
 * 用户信息管理
 * 初始化本地token/mobile等数据
 */
public class UserInfoHelper_boom {

    public static final String SP_KEY_TOKEN = "token";
    public static final String SP_KEY_MOBILE = "phoneNum";
    public static final String SP_KEY_USERID = "userid";
    public static final String SP_KEY_ISADSOURCE = "isAdSource";

    public static final String SP_KEY_NORMALDIALOG_COUNT_boom = "normal_dialog_count";
    public static final String SP_KEY_ACTIVEDIALOG_COUNT_boom = "active_dialog_count";

    public static final String SP_KEY_SHOW_HOLIDAY_WINDOW_DATE = "show_holiday_window_date";

    public static final String SP_KEY_EVENT_POINT = "event_point";

    public static final String LOGIN_TYPE_REGISTER = "REGISTER";
    public static final String LOGIN_TYPE_OLD = "OLD";

    /**
     * 用户是否选择过额度
     */
    public static String USER_HAD_SELECTED_AMOUNT_FLAG = "NO";

    /**
     * 全局sp
     */
    private final SharedPreferences mSharedPreferences;

    /**
     * 登录手机号
     */
    private String mMobile;
    /**
     * 登录token
     */
    private String mToken;
    /**
     * 登录user_id
     */
    private String mUserId;
    /**
     * 是否为渠道用户
     */
    private boolean isAdSource;

    private int normaldialogCount;
    private int activedialogCount;

    private String showHolidayWindowDate;

    private JSONArray eventPointsJsonArray;
    private int evenPointId = 0;

    private static class InnerUserInfoHelper {
        private static final UserInfoHelper_boom instance = new UserInfoHelper_boom();
    }

    public static UserInfoHelper_boom gInstance() {
        return InnerUserInfoHelper.instance;
    }

    private UserInfoHelper_boom() {
        mSharedPreferences = BaseApplication_boom.getInstant_boom().getSharedPreferences(DecHelper_boom.gPackageName(), Context.MODE_PRIVATE);
        this.mMobile = mSharedPreferences.getString(SP_KEY_MOBILE, "");
        this.mToken = mSharedPreferences.getString(SP_KEY_TOKEN, "");
        this.mUserId = mSharedPreferences.getString(SP_KEY_USERID, "");
        this.isAdSource = mSharedPreferences.getBoolean(SP_KEY_ISADSOURCE,false);
        normaldialogCount = mSharedPreferences.getInt(SP_KEY_NORMALDIALOG_COUNT_boom,0);
        activedialogCount = mSharedPreferences.getInt(SP_KEY_ACTIVEDIALOG_COUNT_boom,0);
        showHolidayWindowDate = mSharedPreferences.getString(SP_KEY_SHOW_HOLIDAY_WINDOW_DATE,"");

        try {
            eventPointsJsonArray = new JSONArray(mSharedPreferences.getString(SP_KEY_EVENT_POINT,"[]"));
        } catch (JSONException e) {
            if (Constant_boom.Debug) e.printStackTrace();
            eventPointsJsonArray = new JSONArray();
        }

    }

    public void init(Context context) {}

    public void addEventPoint(String type){
        addEventPoint(type,"");
    }

    public void addEventPoint(String type, String remark){
        JSONObject pointJson = new JSONObject();
        try {
            pointJson.put("type",Constant_boom.PRODUCT+"_"+type);
            pointJson.put("remark", remark);
            pointJson.put("id",evenPointId++);
            pointJson.put("timestamp",String.valueOf(System.currentTimeMillis()));
        }catch (Exception e){
            if (Constant_boom.Debug) e.printStackTrace();
        }
        eventPointsJsonArray.put(pointJson);
        mSharedPreferences.edit().putString(SP_KEY_EVENT_POINT, eventPointsJsonArray.toString()).apply();

        if (eventPointsJsonArray.length()>20){
            HttpManager_boom.getInstance().saveUserPointForBatch();
        }
    }

    public JSONArray gEvenPointsArray(){
        try {
            return new JSONArray(eventPointsJsonArray.toString());
        } catch (JSONException e) {
            return new JSONArray();
        }
    }

    public void clearEvenPoints(){
        eventPointsJsonArray = new JSONArray();
        mSharedPreferences.edit().putString(SP_KEY_EVENT_POINT, "[]").apply();
    }

    public boolean canShowHolidayWindow(){
        return TextUtils.isEmpty(showHolidayWindowDate) || !showHolidayWindowDate.equalsIgnoreCase(LocalDate.now().format(DateTimeFormatter.ISO_DATE));
    }

    public void updateShowHolidayWindowDate(){
        showHolidayWindowDate = LocalDate.now().format(DateTimeFormatter.ISO_DATE);
        mSharedPreferences.edit().putString(SP_KEY_SHOW_HOLIDAY_WINDOW_DATE, this.showHolidayWindowDate).apply();
    }

    //判断是否已经登录
    public boolean didLogin() {
        return !TextUtils.isEmpty(this.mToken);
    }

    public String gMobile() {
        mMobile = Stringutil_boom.getSafeString(mMobile);
        return mMobile;
    }

    public String gHideMobile() {
        return Stringutil_boom.bankAccountHideFront(gMobile());
    }

    @SuppressLint("ApplySharedPref")
    public void sMobile(String mobile) {
        this.mMobile = Stringutil_boom.getSafeString(mobile);
        mSharedPreferences.edit().putString(SP_KEY_MOBILE, this.mMobile).commit();
    }

    public String gToken() {
        mToken = Stringutil_boom.getSafeString(mToken);
        return mToken;
    }

    public String gUserId() {
        mUserId = Stringutil_boom.getSafeString(mUserId);
        return mUserId;
    }

    @SuppressLint("ApplySharedPref")
    public void sToken(String token) {
        this.mToken = Stringutil_boom.getSafeString(token);
        mSharedPreferences.edit().putString(SP_KEY_TOKEN, this.mToken).commit();
    }

    @SuppressLint("ApplySharedPref")
    public void sUserId(String userId) {
        this.mUserId = Stringutil_boom.getSafeString(userId);
        mSharedPreferences.edit().putString(SP_KEY_USERID, this.mUserId).commit();
    }

    public boolean gIsAdSource(){
        return isAdSource;
    }

    @SuppressLint("ApplySharedPref")
    public void sIsAdSource(boolean isAdSource){
        this.isAdSource = isAdSource;
        mSharedPreferences.edit().putBoolean(SP_KEY_ISADSOURCE,isAdSource).commit();
    }

    public int gNormaldialogCount() {
        return normaldialogCount;
    }

    public int gActivedialogCount() {
        return activedialogCount;
    }

    @SuppressLint("ApplySharedPref")
    public void addNormaldialogCount(){
        normaldialogCount++;
        mSharedPreferences.edit().putInt(SP_KEY_NORMALDIALOG_COUNT_boom,normaldialogCount).commit();
    }

    @SuppressLint("ApplySharedPref")
    public void addActivedialogCount(){
        activedialogCount++;
        mSharedPreferences.edit().putInt(SP_KEY_ACTIVEDIALOG_COUNT_boom,activedialogCount).commit();
    }

}
